# -*- coding: utf-8 -*-
"""
Created on Wed Oct 05 14:34:38 2011

@author: Support
"""
try:
    import gaesessions
    
except ImportError:
    try:
        import appengine.gaesessions as gaesessions
    except ImportError:
        class gaesessions:
            @classmethod
            def get_current_session(cls):
                return 'DUMMY Session'

def is_youtube_link(url):
    import re
    youtube_re = re.compile(r'https?://(www\.)?youtube.com/watch\?.*v=.+')
    return bool(youtube_re.match(url)) 


def shorten(url, max_chars = 50):
    import re
    if len(url) < max_chars:
        return url
    url = re.sub(r'https?://(www\.)?', '', url)
    if len(url) < max_chars:
        return url
    return url[:max_chars/2-2] + '...'+ url[-max_chars/2 + 2:]
    
custom_filters = {'shorted': shorten}
custom_tests = {'youtube_link':is_youtube_link}
custom_globals = {'GLOBAL1': 'A'}

def prepare_env( env):
    for name, filter in custom_filters.items():
        env.filters[name] = filter
    for name, test in custom_tests.items():
        env.tests[name] = test
    for name, val in custom_globals.items():
        env.globals[name] = val
    

if __name__ == '__main__':
    
    TEMPLATE_PATH = r'PATH\TO\TEMPLATE\FOLDER'
    OUTPATH = r'PATH\OF\OUTPUT\FILE.py'
    DEBUG = True
    ignore_lst = []#file types to ignore
    use_lst = ['.html','.htm','.tpl']#file types to use as templates
    interval = 5 #how often we check the folder for changes
    watchdog = True #use a watchdog
    
    import tpl2cls
    tpl2cls.run(TEMPLATE_PATH,
            OUTPATH,
            DEBUG,
            ignore_lst,
            use_lst,
            interval,
            watchdog,
            prepare_env,
            'run_tpl2cls')
            # the last parameter is the path to import this module. 
            # it is needed to run prepare_env to support custom filters, tests and globals
            # I originally used this with APPENGINE and the path to this file is 'lib.run_tpl2cls'. if this module is in your PYTHONPATH then the defaul value should suffice.
            
            
